/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifdef WIN32
#  include <math.h>
#  include <time.h>
#  include <windows.h>
#endif

#include "dtime.h"


#if defined (WIN32)

static double round(double x) {
    return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
}

/**
 ****************************************************************************
 * @brief  Get the system time.
 *
 *  Returns the system time as a double precision floating point variable
 *    with a precision on the order of milliseconds.
 *
 * @return Seconds as a double precision floating point variable
 ****************************************************************************
**/
double dtime::getTime_s() {
    static volatile bool initialized_flag = false;
    static time_t epoch = 0;
    static DWORD  ref_ms = 0;
    if (!initialized_flag) {
        epoch = time(NULL);
        ref_ms = timeGetTime();
        initialized_flag = true;
    }

    double t_msec = timeGetTime() - ref_ms;
    double sec = (t_msec / 1e3) + epoch;
    return sec;
}

/**
 ****************************************************************************
 * @brief  Suspend execution for the time specified.
 *
 * @param  time_s  Number of seconds to delay
 *                   specified as a double precision floating point variable.
 ****************************************************************************
**/
void dtime::delaySec(double time_s) {
    Sleep((DWORD) round(time_s * 1000));
}

#else  // WIN32

#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <unistd.h>

double dtime::getTime_s(void) {
    struct timeval t;
    gettimeofday(&t, 0);
    double now_s = t.tv_sec;
    now_s += t.tv_usec / 1e6;
    return now_s;
}

void dtime::delaySec(double time_s) {
    unsigned int sec = (unsigned int) floor(time_s);
    useconds_t  usec = (useconds_t) round((time_s - sec) * 1000000);
    if (sec > 0) {
        sleep(sec);
    }
    usleep(usec);
}

#endif  // WIN32
